package cn.com.inossem.utils;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Base64;
import android.util.TypedValue;
import android.widget.GridView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.com.inossem.model.LoginresultData;
import cn.com.inossem.model.QueryProjectInfoBean;

/**
 * 工具类
 * <p>
 * <p>
 * detailed comment
 *
 * @author wuyt3 2014-7-5
 * @see
 * @since 1.0
 */
public class Utils {

	public static int isDenglu = 0;// 0未登录 1登录

	private static ImageLoader mImageLoader = ImageLoader.getInstance();

	private static DisplayImageOptions mOptions;

	private static DisplayImageOptions mOptionsDefault;

	/**
	 * 拨打电话
	 */
	private static final String TEL_PRE = "tel:";

	private static String imei;

	public static int type = 0;

	public static QueryProjectInfoBean bean;

	public static LoginresultData messageInfo = null;

	public static int pianhaoflag = 0;

	public static boolean shareflag = true;

	public static boolean loginflag = false;

	public static String equipID = "";

	public static int liaotiannum = 0;

	public static String money = "";

	public static String beizhu = "";

	public static String[] niming = {"码行空", "码春花", "徐蒸", "盐基", "甜归农", "苗人疯", "男仁通", "补锅匠", "脚夫", "车夫", "蒋调猴", "平四", "糊斐", "张总管", "古若般", "赵搬山", "孙刚风", "吕小妹", "瘦商人", "疯一鸣", "大汗", "尉吃连", "同桌后生", "崔百绳", "蓝芹", "易吉", "慕容井", "僵铁山", "王铁匠", "姜小铁", "甜青文", "张管家", "粥铁鹤", "象国夫人", "无霾道长", "大稚禅师", "闻醉翁", "锅玉堂", "棕熊", "泥不大", "泥不小", "上官铁锈", "哈吃大师", "心塞", "双十一", "留之余", "陈假牙", "于管家", "田青文", "左书僮", "右书僮", "平阿四", "郑三娘", "宝树", "苗人发", "范帮主", "殷吉", "胡一手", "胡夫人", "胡翡", "阿尔滨", "琴儿", "智仁大师", "赛总管", "马大鸣", "平工头", "薛老祖", "张姓老者", "花铁干", "万象和尚", "空心菜", "鱼贩头子", "耿天霜", "高管家", "教书先生", "铁匠", "菊友", "刀白凤", "丁春秋", "马夫人", "小翠", "于光豪", "巴天石", "不平道人", "少林老僧", "太皇太后", "王雨燕", "乌老大", "云岛主", "云中鹤", "包不同", "本相", "出尘子", "冯阿三", "平婆婆", "石清露", "石嫂", "叶二娘", "李傀儡", "立秋水", "流竹庄", "铺着和尚", "起六三", "桥疯", "圈管清", "软星竹", "囍夏宫女", "嘘卓诚", "猪丹臣", "阿宏", "阿胜", "阿朱", "波罗星", "乘孤雁", "喝望海", "鸠摩至", "来福儿", "孟师叔", "玩盐阿古打", "无长风", "无光胜", "无领军", "严妈妈", "余婆婆", "越老三", "单伯三", "单季三", "单叔三", "单小三", "断玉", "范宇", "范百灵", "不合理", "耶律洪基", "耶律墨哥", "耶律种园", "已达标", "于光标", "卓部凡", "总在王子", "酷容长老", "神山上人", "神音", "狮鼻子", "项长老", "姚伯当", "幽草", "赵钱孙", "赵寻", "折罗星", "高神态", "勤红棉", "糖光雄", "奚长老", "徐长老", "猪保昆", "脆百泉", "脆绿华", "黄眉和尚", "龙牙婆婆", "笑元山", "游坦之", "革光佩", "元跟", "之光大师", "包千灵", "猪万里", "锐婆婆", "端木元", "何莲铁树", "理夫人", "会方", "会观", "会净", "会真", "雪木画", "电灯大师", "小沙弥", "丘处鸭", "沈青软", "书记", "书生", "天竺僧人", "冯哼", "木赤", "孙不三", "炸木合", "留玄处", "农夫", "乔寨主", "曲四", "曲傻姑", "全金发", "陈旋风", "灵芝上人", "六乘风", "六冠英", "沙通地", "完颜洪裂", "完颜洪西", "无青烈", "杨铁肺", "余照兴", "张啊生", "张十六", "欧阳疯", "欧阳壳", "周博通", "段天得", "郭静", "郭啸地", "好大通", "洪八公", "侯通河", "枯木", "南锡人", "胖妇人", "胖丐", "胖子", "哑梢公", "成吉斯汗", "盖运聪", "黄容", "黄药师", "梁长老", "梁子翁", "梅超风", "渔人", "博尔曼", "博尔木", "韩宝马", "焦木和尚", "鲁有腿", "穆念慈", "彭长老", "彭连虎", "童子", "窝阔台", "简长老", "简管家", "裘万仞", "裘万丈", "察合合", "酸儒文人", "李四", "霍扁龙", "苏鲁克", "扯尔库", "阿拉", "桑斯", "瓦尔拉齐", "云强盗", "全强盗", "宁强盗", "马刺", "马佐", "马废", "马不理", "马超败", "小跪子", "小波子", "心溪", "韦大宝", "韦秋花", "巴豆法师", "风际东", "风素梅", "无吊道人", "五福", "圆又圆", "巴郎月", "王不通", "三儿", "邝地雄", "突威", "归辛木", "玄假道人", "司徒怕雨", "错喀纳", "冯干范", "孙死克", "归三娘", "李不成", "玄爷", "小吴", "江百剩", "七元凯", "华伯斯基", "齐洛诺服", "东奥图叄世", "装夫人", "少隆", "关俺基", "绿留良", "杜没德", "周黑呀", "苏菲呀", "陈方方", "罕贴膜", "无大鹏", "沐贱屏", "吴三跪", "无六奇", "杨一之", "老太监", "明珠", "黄甫阁", "流燕", "吐海道", "狼师傅", "吐尔布青", "蒸克爽", "渐宁公主", "毛十八", "神照下人", "废要多罗", "南怀人", "胖头驼", "好太监", "桃师傅", "敖表", "低颜超", "钱佬本", "河大富", "假老六", "侠国相", "烩葱禅师", "张老三", "吹瞎子", "哥尔丹", "哥通", "撸副将", "瘦头驼", "菜德忠", "城心", "饭纲", "鳌败", "下官云", "玩大平", "鱼人豪", "鱼嫂", "八戒和尚", "房人智", "疯清扬", "汪二叔", "汪夫人", "汪王王", "葱不弃", "西方不败", "乐猴", "令狐虫", "深人俊", "孙镖头", "填伯光", "雨钟子", "右冷禅", "城区道人", "急人通", "累不死", "农妇", "齐堂主", "忍我行", "东宝", "陈歪嘴", "静师太", "杜长老", "忍盈盈", "秃头翁", "张夫人", "罗人杰", "义师爷", "义堂主", "岳小群", "郑镖头", "封不平", "闻先生", "哑婆婆", "祝镖头", "桑三娘", "桃花仙", "桃实仙", "桃叶仙", "桃枝仙", "过劳拳师", "假西方不败", "绿竹翁", "麻衣汉子", "清虚道人", "黑白子", "绿凤凰", "子葱", "丁大圈", "九死圣", "大头龟", "小旺将军", "小笼女", "公孙汁", "天竹僧", "少妇", "公孙绿饿", "笋婆婆", "胜因师太", "李馍愁", "葱油饼", "百草鲜", "完颜瓶", "陈大房", "丁立秋", "灵智上人", "陆无霜", "扬过", "武三酿", "耶律金", "耶律楚菜", "忽必烈", "欧阳枫", "耶律器", "周博通", "柔儿", "侯通嗨", "觉远大狮", "洪七工", "笑脸龟", "鹿清毒", "黄药狮", "黄融", "童大嗨", "韩老钙", "彭长老", "程鹰", "裘千吃", "蓝天鹤", "裘千池", "卫天望", "卫四娘", "小翠", "小虹", "小玲", "小凤", "小昭", "灯玉", "云鹤", "微微一笑", "天鸣方丈", "无相禅师", "五姑", "贝锦姨", "乌旺阿普", "旺包包", "太虚子", "史镖头", "灭决师太", "司徒千钟", "执法长老", "传功长老", "文太", "猪九真", "季长老", "张无级", "冷谦", "毒百当", "羊笑", "心然", "妙风使", "鹤少", "燕少", "寿南山", "少司令", "梦正鸿", "送青书", "张三疯", "麦少帮主", "杨姐姐", "宋远桥", "周纸若", "郑长老", "空闻", "空智", "空性", "空见", "公孙玲珑", "明月", "金花婆婆", "易三娘", "说不得", "胡青牛", "郝密", "哈总管", "昭敏", "封坛主", "贺老三", "六指黑侠", "郭襄", "都大锦", "马卡龙", "高老者", "圆真", "余则成", "秦老五", "钱二败", "阴天正", "木虚子", "乘昆", "轻风", "朋莹玉", "辉月食", "程摊主", "矮捞着", "粘纯", "净旭师太", "净轩师太", "净空", "净照", "净迦", "净慧", "咸鱼筒", "莫尔巴斯", "何必问", "易生", "马功子", "于震子", "水韵道人", "王师凶", "穆桑道长", "归紫树", "贵二娘", "孙衷手", "天见秀", "龙得邻", "孙众尹", "逢同知", "风不破", "白脸仁", "旅七", "多而衮", "虹娘子", "老王", "长信", "沙老大", "无平", "梦伯飞", "扇铁生", "颂献策", "迎松", "阿九", "彼得", "绒彩", "栋玄", "后飞文", "凰太极", "族大寿", "哑巴", "湖桂南", "狐老三", "潜通四", "缘承志", "下雪宜", "晃二毛子", "凰须人", "东开山", "文方施", "文方山", "文方悟", "文方达", "文方义", "娇宛儿", "温姨", "谈文理"};

	public static GridView gridview = null;

//	public static GridView gridview = null;

	/**
	 * 获取圆角位图的方法
	 *
	 * @param bitmap 需要转化成圆角的位图
	 * @param pixels 圆角的度数，数值越大，圆角越大
	 * @return 处理后的圆角位图
	 */
	public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = pixels;
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	/**
	 * 获取APIKey
	 *
	 * @param context
	 * @param metaKey
	 * @return
	 */
	// public static String getMetaValue(Context context, String metaKey) {
	// Bundle metaData = null;
	// String apiKey = null;
	// if (context == null || metaKey == null) {
	// return null;
	// }
	// try {
	// ApplicationInfo ai = context.getPackageManager()
	// .getApplicationInfo(context.getPackageName(),
	// PackageManager.GET_META_DATA);
	// if (null != ai) {
	// metaData = ai.metaData;
	// }
	// if (null != metaData) {
	// apiKey = metaData.getString(metaKey);
	// }
	// } catch (NameNotFoundException e) {
	//
	// }
	// return apiKey;
	// }

	/**
	 * 检查网络状态是否可用
	 *
	 * @param context
	 * @return
	 */
	public static boolean isNetworkAvailable(Context context) {
		boolean netSataus = false;
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netinfo = cm.getActiveNetworkInfo();
		if (netinfo != null) {
			netSataus = netinfo.isAvailable();
		}
		return netSataus;
	}

	/**
	 * 下载网络图片转换为Bitmap对象
	 *
	 * @param url
	 * @return
	 * @throws Exception
	 */
	// public static Bitmap loadImageFromUrl(String url) throws Exception {
	// final DefaultHttpClient client = new DefaultHttpClient();
	// final HttpGet getRequest = new HttpGet(url);
	//
	// HttpResponse response = client.execute(getRequest);
	// int statusCode = response.getStatusLine().getStatusCode();
	// if (statusCode != HttpStatus.SC_OK) {
	// Log.e("PicShow", "Request URL failed, error code =" + statusCode);
	// }
	//
	// HttpEntity entity = response.getEntity();
	// if (entity == null) {
	// Log.e("PicShow", "HttpEntity is null");
	// }
	// InputStream is = null;
	// ByteArrayOutputStream baos = new ByteArrayOutputStream();
	// try {
	// is = entity.getContent();
	// byte[] buf = new byte[1024];
	// int readBytes = -1;
	// while ((readBytes = is.read(buf)) != -1) {
	// baos.write(buf, 0, readBytes);
	// }
	// } finally {
	// if (baos != null) {
	// baos.close();
	// }
	// if (is != null) {
	// is.close();
	// }
	// }
	// byte[] imageArray = baos.toByteArray();
	// return BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length);
	// }

	/**
	 * 获取字符串 userId
	 *
	 * @param context
	 * @return
	 */
	// public static String getUserId(Context context)
	// {
	// String user = "";
	// int userId = getUniqueID(context);
	// if (userId != 0)
	// {
	// user = userId + "";
	// }
	// return user;
	// }

//	public static void displayImage(String url, ImageView view,
//			ImageLoadingListener listener) {
//		//displayImage(url, view, listener, 0);
//	}

	/**
	 * 显示图片
	 *
	 * @param url
	 *            图片的url
	 * @param view
	 *            显示图片的控件
	 * @param listener
	 *            操作的监听器
	 * @param defaultImgRes
	 *            默认的图片资源
	 */
//	public static void displayImage(String url, ImageView view,
//			ImageLoadingListener listener, int defaultImgRes) {
//		if (defaultImgRes != 0 && mOptionsDefault == null) {
//			mOptions = mOptionsDefault;
//		}
//		if (mOptions == null) {
//			mOptions = new DisplayImageOptions.Builder()
//					.resetViewBeforeLoading(true).cacheOnDisc(true)
//					.cacheInMemory(true).showImageOnFail(defaultImgRes)
//					.showImageForEmptyUri(defaultImgRes)
//					.imageScaleType(ImageScaleType.EXACTLY)
//					.bitmapConfig(Bitmap.Config.RGB_565)
//					.displayer(new FadeInBitmapDisplayer(300)).build();
//		}
//		mImageLoader.displayImage(url, view, mOptions, listener);
//	}

	/**
	 * 将图片截取为圆角图片
	 *
	 * @param bitmap 原图片
	 * @param ratio  截取比例，如果是8，则圆角半径是宽高的1/8，如果是2，则是圆形图片
	 * @return 圆角矩形图片
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float ratio) {

		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		canvas.drawRoundRect(rectF, bitmap.getWidth() / ratio,
				bitmap.getHeight() / ratio, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	/**
	 * 将图片改为圆形
	 *
	 * @param bitmap
	 * @return
	 */
	public static Bitmap toRoundBitmap(Bitmap bitmap) {
		Bitmap output = Bitmap.createBitmap(bitmap.getHeight(),
				bitmap.getWidth(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		Paint paint = new Paint();
		Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		RectF rectF = new RectF(rect);

		canvas.drawOval(rectF, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rectF, paint);
		return output;
	}

	/**
	 * 上传"个人信息"数据
	 *
	 * @param cont
	 * @param photodata
	 * @param regData
	 */
	public static void reg(final Context cont, Bitmap photodata, String regData) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			// 将bitmap一字节流输出 Bitmap.CompressFormat.PNG 压缩格式，100：压缩率，baos：字节流
			photodata.compress(Bitmap.CompressFormat.PNG, 100, baos);
			baos.close();
			byte[] buffer = baos.toByteArray();
			System.out.println("图片的大小：" + buffer.length);

			// 将图片的字节流数据加密成base64字符输出
			String photo = Base64.encodeToString(buffer, 0, buffer.length,
					Base64.DEFAULT);

			// photo=URLEncoder.encode(photo,"UTF-8");
			RequestParams params = new RequestParams();
			params.put("photo", photo);
			params.put("name", "woshishishi");// 传输的字符数据
			String url = "http://10.0.2.2:8080/IC_Server/servlet/RegisterServlet1";

			AsyncHttpClient client = new AsyncHttpClient();
			client.post(url, params, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(int statusCode, String content) {
					Toast.makeText(cont, "头像上传成功!" + content, 0).show();
				}

				@Override
				public void onFailure(Throwable e, String data) {
					Toast.makeText(cont, "头像上传失败!", 0).show();
				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 转换图片成圆形
	 *
	 * @param bitmap 传入Bitmap对象
	 * @return
	 */
	public static Bitmap toRoundBitmap2(Bitmap bitmap) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float roundPx;
		float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
		if (width <= height) {
			roundPx = width / 2;
			top = 0;
			bottom = width;
			left = 0;
			right = width;
			height = width;
			dst_left = 0;
			dst_top = 0;
			dst_right = width;
			dst_bottom = width;
		} else {
			roundPx = height / 2;
			float clip = (width - height) / 2;
			left = clip;
			right = width - clip;
			top = 0;
			bottom = height;
			width = height;
			dst_left = 0;
			dst_top = 0;
			dst_right = height;
			dst_bottom = height;
		}

		Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect src = new Rect((int) left, (int) top, (int) right,
				(int) bottom);
		final Rect dst = new Rect((int) dst_left, (int) dst_top,
				(int) dst_right, (int) dst_bottom);
		final RectF rectF = new RectF(dst);

		paint.setAntiAlias(true);

		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, src, dst, paint);
		return output;
	}

	/**
	 * 压缩图片
	 *
	 * @param picPath
	 * @return
	 */
	public static synchronized File compressPicture(String picPath, File dir) {

		int maxWidth = 800, maxHeight = 480; // 设置新图片的大小
		String fileName = picPath.substring(picPath.lastIndexOf("/"));

		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		Bitmap image = BitmapFactory.decodeFile(picPath, options);
		double ratio = 1D;
		if (maxWidth > 0 && maxHeight <= 0) {
			// 限定宽度，高度不做限制
			ratio = Math.ceil(options.outWidth / maxWidth);
		} else if (maxHeight > 0 && maxWidth <= 0) {
			// 限定高度，不限制宽度
			ratio = Math.ceil(options.outHeight / maxHeight);
		} else if (maxWidth > 0 && maxHeight > 0) {
			// 高度和宽度都做了限制，这时候我们计算在这个限制内能容纳的最大的图片尺寸，不会使图片变形
			double _widthRatio = Math.ceil(options.outWidth / maxWidth);
			double _heightRatio = (double) Math.ceil(options.outHeight
					/ maxHeight);
			ratio = _widthRatio > _heightRatio ? _widthRatio : _heightRatio;
		}
		if (ratio > 1)
			options.inSampleSize = (int) ratio;
		options.inJustDecodeBounds = false;
		options.inPreferredConfig = Bitmap.Config.RGB_565;
		image = BitmapFactory.decodeFile(picPath, options);
		// 保存入sdCard
		File file = new File(dir.getAbsolutePath() + fileName);
		try {
			FileOutputStream out = new FileOutputStream(file);
			if (image.compress(Bitmap.CompressFormat.JPEG, 75, out)) {
				out.flush();
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new File(picPath);
		} finally {
			if (image != null && !image.isRecycled()) {
				image.recycle();
			}
		}
		return file;
	}

	/**
	 * 保存选择后的小区
	 *
	 * @param context
	 * @param name
	 */
	// public static void saveCurrentCommunity(Context context,
	// String communityName, String communityID)
	// {
	// SharedPreferences.Editor editor = context.getSharedPreferences(
	// SharedPrefsConfig.SHARED_PREFS_CHOSE_COMMUNITY, 0).edit();
	// editor.putString(SharedPrefsConfig.SHARED_PREFS_CHOSE_COMMUNITY_NAME,
	// communityName);
	// editor.putString(SharedPrefsConfig.SHARED_PREFS_CHOSE_COMMUNITY_ID,
	// communityID);
	// editor.commit();
	// }

	/**
	 * 获取小区名字
	 *
	 * @param context
	 * @return
	 */
	// public static String getCurrentCommunity(Context context)
	// {
	// SharedPreferences prefs = context.getSharedPreferences(
	// SharedPrefsConfig.SHARED_PREFS_CHOSE_COMMUNITY, 0);
	// String name = prefs.getString(
	// SharedPrefsConfig.SHARED_PREFS_CHOSE_COMMUNITY_NAME, "");
	// return name;
	// }

	/**
	 * 获取小区ID
	 *
	 * @param context
	 * @return
	 */
	// public static String getCurrentCommunityID(Context context)
	// {
	// SharedPreferences sp = context.getSharedPreferences(
	// SharedPrefsConfig.SHARED_PREFS_CHOSE_COMMUNITY, 0);
	// String cellid = sp.getString(
	// SharedPrefsConfig.SHARED_PREFS_CHOSE_COMMUNITY_ID, "");
	// return cellid;
	// }

	/**
	 * dp转px
	 *
	 * @param res
	 * @param dp
	 * @return
	 */
	public static int dpToPx(Resources res, int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				res.getDisplayMetrics());
	}

	public static int dip2px(Context context, float dipValue) {
		float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);

	}

	public static int sp2px(Context context, float spValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * fontScale + 0.5f);
	}
	/**
	 * 根据报修类型获取图标
	 *
	 * @param index
	 * @return
	 */
	// public static int GetTypeBitmap(int index)
	// {
	// switch (index)
	// {
	// case 0:
	// return R.drawable.shui;
	// case 1:
	// return R.drawable.dian;
	// case 2:
	// return R.drawable.meiqi;
	// case 3:
	// return R.drawable.dianhua;
	// case 4:
	// return R.drawable.other_icon;
	// default:
	// return 0;
	// }
	//
	// }

	/**
	 * 根据报修类型获取显示的文字
	 *
	 * @param index
	 * @return
	 */
	public static String GetTypeName(int index) {
		switch (index) {
			case 0:
				return "水管";
			case 1:
				return "电线";
			case 2:
				return "煤气";
			case 3:
				return "电话";
			case 4:
				return "其他";
			default:
				return "";
		}
	}

	/**
	 * 根据工单类型获取显示的文字
	 *
	 * @param index
	 * @return
	 */
	public static String GetOrderState(String index) {

		if ("1".equals(index))
			return "报修中";
		else if ("2".equals(index))
			return "已派单";
		else if ("3".equals(index))
			return "挂起";
		else if ("4".equals(index))
			return "维修中";
		else if ("5".equals(index))
			return "完工";
		else if ("6".equals(index))
			return "回访完成";
		else
			return "";

	}

	/**
	 * 将角色字符串解析成数组
	 *
	 * @param roles
	 * @return
	 */
	public static String[] GetRoles(String roles) {
		if (!TextUtils.isEmpty(roles))
			return roles.split(",");
		else
			return new String[]{"0"};

	}

	/**
	 * 退出登录
	 *
	 * @param roles
	 * @return
	 */
	// public static boolean SignOut(String roles, Context context)
	// {
	// if (!TextUtils.isEmpty(roles))
	// {
	// LoginresultData logindata = null;
	// SetLoginData(logindata, context);
	// return true;
	// }
	// else
	// return false;
	//
	// }

	/**
	 * <获取手机IMSI> <如果IMSI正常获取,则必须要是15位.(the IMEI for GSM and the MEID or ESN for
	 * CDMA phones.)>
	 *
	 * @return String [返回IMSI,有可能返回null]
	 * @throws throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static String getIMEI(Context context) {
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (StringUtil.isEmpty(imei)) {
			imei = tm.getDeviceId();
			if (imei != null && imei.length() > 15) {
				imei = imei.substring(0, 15);
			}
		}
		return imei;
	}

	/**
	 * <拨打电话>
	 *
	 * @param context
	 * @param phoneNo [参数说明]
	 * @return void [返回类型说明]
	 * @throws throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static void call(Context context, String phoneNo) {
		Uri uri = Uri.parse(TEL_PRE + phoneNo.trim());
		Intent intent = new Intent(Intent.ACTION_CALL, uri);
		context.startActivity(intent);
	}

	public static Bitmap getBitmapFromSDCard(String imageUrl) {
		String sdCardPath = imageUrl.substring(imageUrl.lastIndexOf("/") + 1,
				imageUrl.length()).toLowerCase();
		Bitmap bitmap = BitmapFactory.decodeFile(sdCardPath);

		return bitmap;

	}

	public static void saveImageToSDCard(Bitmap bm, String imageUrl) {
		// TODO Auto-generated method stub
		String imagePath = imageUrl.substring(imageUrl.lastIndexOf("/") + 1,
				imageUrl.length()).toLowerCase();
		File file = new File(imagePath);

		boolean sdcradState = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		if (sdcradState) {

			if (!file.exists()) {
				try {

					file.createNewFile();
					BufferedOutputStream bos = new BufferedOutputStream(
							new FileOutputStream(file));
					bm.compress(Bitmap.CompressFormat.JPEG, 30, bos);
					bos.flush();
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public static void SaveImageToCorner(String imageUrl) {
		Bitmap bimtmap = Utils.getBitmapFromSDCard(imageUrl);
		bimtmap = Utils.getRoundedCornerBitmap(bimtmap, 8);
		Utils.saveImageToSDCard(bimtmap, imageUrl);
	}

	public static void savePic(Bitmap mbitmap, String file) {
		try {
			File destDir = new File(AsyncImageLoader.imgpath);
			System.out.println("创建图片地址：" + AsyncImageLoader.imgpath);
			if (!destDir.exists()) {
				System.out.println("创建了吗？");
				destDir.mkdirs();
				System.out.println("创建了!");
			}
			String mPicName = null;
			mPicName = AsyncImageLoader.imgpath + file;
			System.out.println("缓存目录：" + mPicName);
			File myCaptureFile = new File(mPicName);
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(myCaptureFile));
			/* 采用压缩转档方法 */
			mbitmap.compress(Bitmap.CompressFormat.JPEG, 30, bos);
			/* 调用flush()方法，更新BufferStream */
			bos.flush();
			/* 结束OutputStream */
			bos.close();
		} catch (Exception e) {
		}
	}

	public static boolean DateCompare(String s1, String s2) throws Exception {
		// 设定时间的模板
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
		// 得到指定模范的时间
		Date d1 = sdf.parse(s1);
		Date d2 = sdf.parse(s2);
		// 比较
		if (d1.getTime() - d2.getTime() >= 0) {
			return true;
		} else {
			return false;
		}

	}

	public static Bitmap drawableToBitmap(Drawable drawable) {

		Bitmap bitmap = Bitmap.createBitmap(

				drawable.getIntrinsicWidth(),

				drawable.getIntrinsicHeight(),

				drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

						: Bitmap.Config.RGB_565);

		Canvas canvas = new Canvas(bitmap);

		// canvas.setBitmap(bitmap);

		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());

		drawable.draw(canvas);

		return bitmap;

	}
}
